package com.hbnu.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hbnu.mapper.UserMapper;

import com.hbnu.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.security.SecureRandom;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    //生成验证码
    public static String generateCaptchaCode(int length) {
        String chars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
        SecureRandom rnd = new SecureRandom();
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            sb.append(chars.charAt(rnd.nextInt(chars.length())));
        }
        return sb.toString();
    }

    //修改密码
    public boolean updatePassword(String username, String oldPassword, String newPassword) {
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("username", username));
        if (user == null) {
            return false;
        }
        if (!user.getPassword().equals(oldPassword)) {
            return false;
        }
        user.setPassword(newPassword);
        int result = userMapper.updateById(user);
        return result > 0;
    }

}
